package com.itheima.controller;


import com.itheima.common.DefaultException;
import com.itheima.common.Paging;
import com.itheima.common.R;
import com.itheima.dto.message.MessageDto;
import com.itheima.dto.message.MessagePageDto;
import com.itheima.service.MessageService;
import com.itheima.vo.KindVo;
import com.itheima.vo.message.MessageBoardVo;
import com.itheima.vo.message.MessageSoaringVo;
import com.itheima.vo.message.MessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * (Message)表控制层
 *
 * @author makejava
 * @since 2022-10-01 22:01:15
 */
@RestController
@RequestMapping("message")
@Api(tags = "信息操作api")
@Validated
public class MessageController {
    @Resource
    private MessageService messageService;

    /**
     * 发表信息
     *
     * @param messageDto 发布信息dto
     * @return 结果
     * @throws Exception 异常
     */
    @ApiOperation(value = "发表信息Api")
    @PostMapping("/sendMessage")
    public R<String> sendMessage(@Validated @RequestBody MessageDto messageDto) throws Exception {
      messageService.sendMessage(messageDto);
        return R.success("success");
    }

    /**
     * 分页展示信息
     * @param messagePageDto 分页查看信息dto（按条件筛选）
     * @return 返回信息vo
     * @throws Exception 异常
     */
    @ApiOperation(value = "分页展示信息Api", notes = "{\n" +
            "    \"kinds\": [\n" +
            "        2,\n" +
            "        3\n" +
            "    ],\n" +
            "    \"keywords\": \"内容\",\n" +
            "    \"page\": 0,\n" +
            "    \"limit\": 2\n" +
            "}")
    @PostMapping("/pageMessage")
    public R<Paging<MessageVo>> pageMessage(@RequestBody MessagePageDto messagePageDto) throws Exception {
        return R.success(messageService.pageMessage(messagePageDto));
    }

    /**
     * 分页展示个人信息
     *
     * @param messagePageDto 分页查看信息dto（按条件筛选）
     * @return 返回信息vo
     */
    @ApiOperation(value = "分页展示个人信息Api", notes = "示例：{\n" +
            "    \"kinds\": [\n" +
            "        2,\n" +
            "        3\n" +
            "    ],\n" +
            "    \"keywords\": \"内容\",\n" +
            "    \"page\": 0,\n" +
            "    \"limit\": 2\n" +
            "}")
    @PostMapping("/pagePersonalMessage")
    public R<Paging<MessageVo>> pagePersonalMessage(@ApiParam("MessagePageDto") @RequestBody MessagePageDto messagePageDto) throws Exception {
        return R.success(messageService.pagePersonalMessage(messagePageDto));
    }

    /**
     * 批量删除信息
     *
     * @param messageIds 信息id集合
     * @return 结果
     */
    @ApiOperation(value = "删除信息", notes = "示例" +
            "[1,2]")
    @DeleteMapping("/deleteMessage")
    public R<String> deleteMessage(@RequestParam("messageIds") List<Integer> messageIds) throws Exception {
        return R.success(messageService.deleteMessage(messageIds));
    }

    /**
     * 信息点赞功能
     *
     * @param messageId 信息id
     * @return 结果
     */
    @ApiOperation(value = "信息点赞功能")
    @GetMapping("/likeMessage")
    public R<String> likeMessage(@NotNull @RequestParam("messageId") Integer messageId) throws Exception {
        messageService.likeMessage(messageId);
        return R.success("点赞成功");
    }

    /**
     * 取消点赞功能
     *
     * @param messageId 信息id
     * @return 结果
     * @throws Exception 异常
     */
    @ApiOperation(value = "取消点赞功能")
    @DeleteMapping("/cancelLikeMessage")
    public R<String> cancelLikeMessage(@RequestParam(value = "messageId") Integer messageId) throws Exception {
        messageService.cancelLikeMessage(messageId);
        return R.success("取消点赞成功");
    }

    /**
     * 查找种类
     *
     * @return 种类vo
     */
    @ApiOperation(value = "查找种类")
    @GetMapping("/findKind")
    public R<List<KindVo>> findKind() {
        return R.success(messageService.findKind());
    }

    /**
     * 新增种类
     *
     * @param type 种类
     * @return 结果
     */
    @ApiOperation(value = "新增种类")
    @GetMapping("/addKind")
    public R<String> addKind(@NotBlank(message = "种类不能为空") @RequestParam("type") String type) throws Exception {
        messageService.addKind(type);
        return R.success("新增种类成功");
    }

    /**
     * 删除种类
     * @param name 种类
     * @return 结果
     * @throws Exception 异常
     */
    @ApiOperation(value = "删除种类")
    @DeleteMapping("/deleteKind")
    public R<String> deleteKind(@NotNull(message = "种类不能为空") @RequestParam("name") String name) throws Exception {
        messageService.deleteKind(name);
        return R.success("删除种类成功");
    }

    /**
     * 信息点赞排行榜
     *
     * @return 信息点赞排行榜vo
     */
    @ApiOperation(value = "信息点赞排行榜")
    @GetMapping("/likeLeaderboard")
    public R<List<MessageBoardVo>> likeLeaderboard() throws Exception {
        return R.success(messageService.likeLeaderBoard());
    }

    /**
     * 信息点赞飙升榜
     *
     * @return 信息点赞飙升vo
     */
    @ApiOperation(value = "信息点赞飙升榜")
    @GetMapping("/likeSoaring")
    public R<List<MessageSoaringVo>> likeSoaring() throws Exception {
        return R.success(messageService.likeSoaring());
    }

    /**
     * 图片/视频预览
     * @param files 图片/视频文件
     * @return url
     */
    @ApiOperation(value = "图片/视频预览")
    @PostMapping("/preview")
    public R<List<String>> preview(@RequestParam("image") List<MultipartFile> files) throws DefaultException {
        return R.success(messageService.preview(files));
    }

    /**
     * 获取新闻
     * @param id 信息id
     * @return 返回信息vo
     */
    @ApiOperation(value = "获取新闻")
    @GetMapping("/getMessage")
    public R<MessageVo> getMessage(@RequestParam("id")Integer id){
        return R.success(messageService.getMessage(id));
    }
}

